package com.lcf.app.hp.config;

import lombok.Getter;
import lombok.Setter;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Setter
@Getter
@Configuration
@ConfigurationProperties(prefix = "proxy")
public class HttpProxyConfig {

    private static final String PROXY_NAME = "DEFAULT_PROXY_NAME";

    //当前服务提供的url
    private String servletUrl;
    //需要代理的uri
    private String targetUri;
    //是否打印日志
    private boolean enableLog = true;

    /**
     * 代理服务
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean registrationBean() {
        //可以自定义实现 ProxyServlet，完成业务逻辑
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new ProxyServlet(), servletUrl);
        registrationBean.setName(PROXY_NAME);
        //代理地址
        registrationBean.addInitParameter(ProxyServlet.P_TARGET_URI, targetUri);
        //代理转发的日志
        registrationBean.addInitParameter(ProxyServlet.P_LOG, String.valueOf(enableLog));
        return registrationBean;
    }

}
